<?php

class CA_Menu
{
	public function init()
	{
		$sql = &$GLOBALS['sql'];
		
		# Getting menu structure
		$q = $sql->query("SELECT * FROM menu_template LIMIT 1");
		
		$tpl = $sql->fetchArray($q, MYSQL_ASSOC);
		$sql->freeResult($q);
		
		# Getting menu links.
		$q = $sql->query("SELECT title, url FROM menu WHERE active = 1 ORDER BY priority ASC");
		
		$links = array();
		$str_links = "";
		
		while($row = $sql->fetchArray($q, MYSQL_ASSOC))
		{
			$links[] = $row;
		}
		
		foreach ($links as $link)
		{
			$url = $link['url'];
			
			if(!preg_match("#^(http|https|svn|irc|ftp|wow)://([-A-Z0-9.]+)#si", $link['url']))
			{
				$address = $GLOBALS['info']['site_address'];
				
				if(substr($address, strlen($address)-1) !== "/")
				{
					$address .= "/";
				}
				
				$url = $address . $link['url'];
			}
			
			$str_links .= str_replace(array("{TITLE}", "{URL}"), array($link['title'], $url), $tpl['link_structure']);
		}
		
		$menu = str_replace("{MENU}", $str_links, $tpl['menu_wrap']);
		
		$GLOBALS['tplBody']->assign("menu", $menu);
	}
}

$GLOBALS['hc']->registerHook("CA_MENU_GET");

?>